load D:\temp\subjects\biopac-all-trials-variable-window-shift20

n_subjects = length(unique(S));

n_channels = 10;
n_classes = 12;
n_trials_per_class = 20;
%n_subjects = size(dataOut,2) / n_trials_per_class;
%n_subjects = 6;

classes = [1:n_classes];

potatos = [];
subjects_to_process = randperm(n_subjects);

% for test
%subjects_to_process = [1:9];
%n_subjects = 9;

accuracy =0;
total_test_trials = 0;
    

for i = 1:n_subjects
%for i = 1:1

    % Training
    
%     temp = subjects_to_process(1);
%     subjects_to_process(1) = subjects_to_process(i);
%     subjects_to_process(i) = temp;
    
    %subjects_to_process(2:n_subjects)
    
    testSubjectLabel = subjects_to_process(i);
    
    for c=classes

            TrainCOV = X(:,:,Y==c & (S~=testSubjectLabel));

            %[C, th] = potato_estimation_iterativ(TrainCOV);
            [C, th] = potato_estimation(TrainCOV);

            potato.classNumber = c;
            potato.baryCenter = C;
            potato.threshold = th;

            potatos = [potatos potato];
    end

    disp('Train completed');

   % Classification

    classAdjustmentsCount = 0;

    %subjects_to_process(1)
    for c = classes

       %for s=testSubjectLabel

          XCov = X(:,:,Y==c & (S==testSubjectLabel));
          
          for t=1:n_trials_per_class

%                %if (t>3 && s>9) 
% %                if (s>9) 
% %                    continue; 
% %                end;
% 
%               buffer = dataOut{c, (s-1) * 10 + t }(:,1:8);
%               XTest = covariances(buffer');
              XTest = XCov(:,:,t);

              min_norm_dist = 1000; % minimum normalized distance
              best_class = 0;

              for m = 1:(length(potatos))

                    [~,d] = potato_detection(XTest,potatos(m).baryCenter,potatos(m).threshold);

                    th = potatos(m).threshold;

                    if (d < th) % it is in the class, distance is less than the threshold

                        %check for better class
                        norm_dist = d / th; % normalize

                        if (norm_dist < min_norm_dist)

                             if (min_norm_dist ~= 1000) % we found a change between two classes and not a simple assignment of change
                                 %disp(['Class adjusted from ' int2str(best_class) ' to ' int2str(m)]);
                                 classAdjustmentsCount = classAdjustmentsCount + 1;
                             end;

                             min_norm_dist = norm_dist;
                             best_class = m;
                        end;
                    end;

              end;

              %result(t) = best_class;

              total_test_trials = total_test_trials + 1;
              
              if (best_class == c)
                  accuracy = accuracy + 1; 
              end;
          end; % trials

  %      end; % subjects
    end; % classes
    disp('Classification completed');
end; % subjects iteration


accuracy / total_test_trials
classAdjustmentsCount